package org.hitchhike.core.repository;

import java.util.Set;

import org.hitchhike.common.repository.BaseRepository;
import org.hitchhike.core.domain.OrderDetail;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface OrderDetailRepository extends BaseRepository<OrderDetail, Long> {

  @Query("select od from OrderDetail od where od.user.id = :userId and od.state in(:states)")
  Page<OrderDetail> findPage(@Param("userId") Long userId, @Param("states") Set<Integer> states,
      Pageable pageable);


  /**
   * 检查车票是否取消
   * 
   * @return
   */
  @Query("select count(1) from OrderDetail od where (od.state = 1 or od.state = 2) and od.order.id = :orderId")
  int checkCancelTicket(@Param("orderId") Long orderId);

  /**
   * 检查车票是否付款
   * 
   * @return
   */
  @Query("select count(1) from OrderDetail od where od.state = 1 and od.order.id = :orderId")
  int checkPaymentTicket(@Param("orderId") Long orderId);

  /**
   * 修改订单明细-取消
   */
  @Modifying
  @Query("update OrderDetail set state = 0 where order.id = :orderId")
  public void updateCancel(@Param("orderId") Long orderId);

  /**
   * 
   * 修改订单明细-支付
   */
  @Modifying
  @Query("update OrderDetail set state = 2 where order.id = :orderId")
  public void updatePayment(@Param("orderId") Long orderId);
}
